const Service = require('../../../core/service/ApiService');

class AcccountSvc extends Service {

    //登录
    async login(account, pwd, remember) {
        const dao = this.service.tablet.account.dao;
        const act = await dao.findAccountByAccount(account);
        if (!act)
            throw new this.error.BusinessError('账号不存在');
        const encryptedPwd = this.helper.sha1(pwd);
        if (encryptedPwd !== act.pwd)
            throw new this.error.BusinessError('密码错误');
        this.ctx.session.account = act.account;
        this.ctx.session.town_id = act.town_id;
        if (remember) {
            this.ctx.session.maxAge = 604800000;  //7天
        }
        return {
            town_name: act.town_name
        };
    }

    //退出登录
    async logout() {
        this.ctx.session = null;
    }

    //查询小区列表
    async getSubList() {
        return {
            subdistrict_list: await this.service.tablet.account.dao.getSubList()
        }
    }
}

module.exports = AcccountSvc;
